
import {get_target_server} from "/hack_prepare/get-target-server.js"

var GNS = {};

async function update()
{
    let ns = GNS;

    // 取目标服务器:
    let target_server = await get_target_server(ns);

    let windows = document.querySelectorAll(".MuiPaper-root");
    ns.print("windows:",windows.length,"个");
    // 取有tbody的容器:
    let overview_window = undefined;
    // 并取得对应的tbody:
    let contain_tbody = undefined;
    for(let i=0;i<windows.length;++i)
    {
        let a_window = windows[i];
        let list_tbody = a_window.querySelectorAll("tbody");
        let number_of_tbody = list_tbody .length;
        ns.print(number_of_tbody);
        if(number_of_tbody)
        {
            overview_window= a_window;
            contain_tbody = list_tbody[0];
            break;
        }
    }
    // 在列表中插入一个tr
    // 在原有的容器中找tr
    let exist_tr = contain_tbody.querySelector("#target-server");
    let target_tr = undefined;
    if(exist_tr)
    {
        ns.print("已存在#target-server");
        target_tr = exist_tr;//contain_tbody.query
        target_tr.innerHTML = "目标服务器:"+target_server;
        // css样式:
        target_tr.classList.add('css-fupmec');
    }
    else
    {
        // 生成tr标签
        var newTr = document.createElement('tr');
        newTr.id = 'target-server';
        // 加入标签
        contain_tbody.appendChild(newTr);
        newTr.innerHTML = "目标服务器:"+target_server;
        // css样式:
        newTr.classList.add('css-fupmec');
        target_tr = newTr;
        // 自定义样式:
        newTr.style.cssText = "font-size: .8rem;";
    }

    // 进度条1:grow
    let progress_bar_grow;
    const id_grow = "grow-progress"
    let exist_grow = contain_tbody.querySelector("#"+ id_grow);
    if(exist_grow)
    {
        ns.print("已存在#",id_grow);
        progress_bar_grow = exist_grow;
    }
    else
    {
        let neo_tr = document.createElement('tr');
        neo_tr.classList.add('MuiTableRow-root');
        neo_tr.classList.add('css-1dix92e');
        neo_tr.id=id_grow;
        let neo_grow = document.createElement('th');
        neo_tr.append(neo_grow);
        neo_grow.classList.add('MuiTableCell-root');
        neo_grow.classList.add('jss11');
        neo_grow.classList.add('MuiTableCell-body');
        neo_grow.classList.add('MuiTableCell-sizeMedium');
        neo_grow.classList.add('css-hadb7u');
        neo_grow.style.cssText = "position: relative;top: -3px;";
        neo_grow.scope="row";
        // neo_grow.setAttribute('scope', 'row');
        neo_grow.colSpan = '2';
        // neo_grow.setAttribute('colSpan', '2');
        let neo_span = document.createElement('span');
        neo_span.classList.add('MuiLinearProgress-root');
        neo_span.classList.add('MuiLinearProgress-colorPrimary');
        neo_span.classList.add('MuiLinearProgress-determinate');
        neo_span.classList.add('css-koo86v');
        neo_span.style.backgroundColor="rgb(37, 37, 37)"
        neo_grow.appendChild(neo_span);
        let neo_span_inner = document.createElement('span');
        neo_span_inner.classList.add('MuiLinearProgress-bar');
        neo_span_inner.classList.add('MuiLinearProgress-barColorPrimary');
        neo_span_inner.classList.add('MuiLinearProgress-bar1Determinate');
        neo_span_inner.classList.add('css-14usnx9');
        neo_span_inner.style.transform = "translateX(-50%)";
        neo_span.appendChild(neo_span_inner);
        
        // 加入标签
        contain_tbody.appendChild(neo_tr);
        // 更新变量:
        progress_bar_grow = neo_tr;
    }
    
    // 更新进度条grow:
    {
        let max_money = ns.getServerMaxMoney(target_server);
        let available_money = ns.getServerMoneyAvailable(target_server);
        let span_move_rate = available_money/max_money;
        span_move_rate = "-"+(100-span_move_rate*100)+'%';
        // 取节点
        let elm_span = progress_bar_grow.querySelector(".MuiLinearProgress-bar");
        elm_span.style.transform = "translateX("+ span_move_rate +")";
    }


}

/** 
 * 显示正在黑的目标服务器的情况
 * 
 */
export async function main(ns) {
    ns.tail("/hack_prepare/overview-target-server.js");
    GNS = ns;
    
    while(true)
    {
        await update();
        await ns.sleep(1000);
    }


}